/*
 * SimpleHClass.java
 *
 * Created on October 4, 2007, 2:48 PM
 *
 */

package org.osdtsystem.hierarchy;

/**
 * A simple class for hierarchies such as tree hierarchies.
 * @author Martin Haulrich and Matthias Buch-Kromann
 */
public final class SimpleHClass extends AbstractHClass {
    final public static String EMPTYCLASSNAME = "_EMPTY_";
    final public static String TOPCLASSNAME = "_TOP_";
    private final String name;
    private final int hash;
    private final int depth;
    
    /**
     * Constructor
     * @param name the unique name of the class
     */
    SimpleHClass(String name, int depth) {
        this.name = name;
        this.depth = depth;
        this.hash = name.hashCode();
    }    

    SimpleHClass(boolean topClass) {
        this.name = null;
        this.depth = topClass ? 0 : Integer.MAX_VALUE;
        this.hash = 0;
    }
    
    @Override
    public int depth() {
        return depth;
    }

    @Override
    public String toString() {
        if (name != null) {
            return name;
        } else {
            return depth == 0 ? TOPCLASSNAME : EMPTYCLASSNAME;
        }
    }


    @Override
    public int hashCode() {
      return hash;
    }

    @Override
    public boolean equals(Object o) {
        if (! (o instanceof SimpleHClass))
            return false;
        SimpleHClass that = (SimpleHClass) o;
        return this.hash == that.hash 
                && ((this.name != null && this.name.equals(that.name))
                    || this == that);
    }
}
